package com.blueocean_health.care.management.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.UserAccount;
import com.blueocean_health.care.management.domain.vo.UserVo;
import com.blueocean_health.care.management.service.UserAccountService;

/**
 * 用户模块  2018年4月18日11:02:21 
 * @author wuyanli
 *
 */
//@RestController
//@RequestMapping("user")
public class UserAccountController {
	
	@Resource
	private UserAccountService userService;

	/**
	 * 新增用户  2018年4月18日11:04:38
	 * @param userAccount
	 * @return
	 */
	@RequestMapping("add")
	public SysResult saveUser(@RequestBody UserAccount userAccount){
		return userService.addUser(userAccount);
	}
	/**
	 * 编辑用户信息  2018年4月18日11:19:51
	 * @param userAccount
	 * @return
	 */
	@RequestMapping("update")
	public SysResult updateUser(@RequestBody UserAccount userAccount){
		return userService.updateUser(userAccount);
	}
	/**
	 * 重置密码 2018年4月18日11:19:59
	 * @param userAccount
	 * @return
	 */
	@RequestMapping("reset")
	public SysResult resetPassword(@RequestBody Map<String, String> map){
		return userService.updatePassword(map);
	}
	
	/**
	 * 删除用户
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("delete")
	public SysResult delete(@RequestBody Map<String, String> map) {
		return userService.deleteUser(map.get("userId"));
	}
	
	/**
	 * 查看用户关联角色
	 * @return
	 */
	@RequestMapping("role/list")
	public SysResult queryRole(@RequestBody Map<String, String> map) {
		return userService.queryRole(map.get("userId"));
	}
	
	/**
	 * 查看用户详情
	 * @param map
	 * @return
	 */
	@RequestMapping("query")
	public SysResult query(@RequestBody Map<String, String> map) {
		return userService.query(map.get("userId"));
	}
	
	/**
	 * 查看用户列表
	 * @param map
	 * @return
	 */
	@RequestMapping("list")
	public SysResult list(@RequestBody Map<String, Object> map) {
		return userService.list(map);
	}
	
	/**
	 * 当前用户组织下的组织/项目架构图
	 * @param map
	 * @return
	 */
	@RequestMapping("team/list")
	public SysResult teamList() {
		return userService.teamList();
	}
	
	/**
	 * 登录
	 * @return
	 */
//	@RequestMapping("login")
	public SysResult login(@RequestBody UserAccount userAccount) {
		if (StringUtils.isEmpty(userAccount.getUserName())){
			return SysResult.error(-1, "用户名不能为空");
		} else if (StringUtils.isEmpty(userAccount.getPassword())) {
			return SysResult.error(-1, "密码不能为空");
		} else {
			UserVo currentUserInfo = userService.login(userAccount);
			if(currentUserInfo==null){
				return SysResult.error(-1, "用户名或密码不正确");
			}
			return SysResult.ok(currentUserInfo);
		}
	}
	
	/**
	 * 登录后多身份选择
	 * @param map
	 * @return
	 */
//	@RequestMapping("login/select-identity")
	public SysResult selectIdentity(@RequestBody Map<String, String> map, HttpServletRequest request) {
		String teamId = map.get("teamId");
		String token = request.getHeader("token");
		if (StringUtils.isEmpty(token)) {
			return SysResult.error("请求参数异常");
		}
		Map<String,String> paramsMap = new HashMap<>();
		paramsMap.put("teamId", teamId);
		paramsMap.put("token", token);
		return userService.selectIdentity(paramsMap);
	}
	
}
